package com.teacold.customdemo.chapter_one;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

public class DrawText extends View {
    private Paint mPaint;
    private Context mContext;

    public DrawText(Context context) {
        super(context);
        init(context);
    }

    public DrawText(Context context, AttributeSet attrs) {
        this(context);
    }

    public DrawText(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }


    private void init(Context context) {
        mPaint = new Paint();
        mContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        customFont(canvas);

    }

    /**
     * 自定义的文字字体   没有成功
     */
    private void customFont(Canvas canvas) {
        mPaint.setStrokeWidth(5);
        mPaint.setTextSize(40);
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.FILL);
        AssetManager manager = mContext.getAssets();
        Typeface typeface = Typeface.createFromAsset(manager, "ZITI.TTF");
        mPaint.setTypeface(typeface);
        canvas.drawText("山无棱，乃敢与君绝！", 10, 100, mPaint);
    }

    private void drawTextPath(Canvas canvas) {
        mPaint.setStrokeWidth(5);
        mPaint.setTextSize(40);
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.FILL);
        Path path = new Path();
        path.addCircle(220, 300, 100, Path.Direction.CCW);
        canvas.drawPath(path, mPaint);
        String s = "山水无心，人有意，东风吹破";
        mPaint.setColor(Color.BLUE);
        canvas.drawTextOnPath(s, path, 0, 0, mPaint);


    }

    /**
     * 文字指定位子绘制
     * pos[]的size 数应大于等于2倍的String数
     */
    private void drawtpos(Canvas canvas) {
        float[] pos = {10, 50, 30, 56, 92, 84, 63, 48, 54, 203,};
        String string = "山河依";
        mPaint.setStrokeWidth(5);
        mPaint.setTextSize(40);
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawPosText(string, pos, mPaint);

    }

    /**
     * 文字的绘制
     */
    private void drawt(Canvas canvas) {
        String string = "山河依旧在，空留故人心";
        mPaint.setStrokeWidth(5);
        mPaint.setTextSize(40);
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(string, 2, 5, 100, 200, mPaint);

    }


    /**
     * 简单的draw文字
     */
    private void drawText(Canvas canvas) {
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(100);
        mPaint.setTextAlign(Paint.Align.CENTER);//居中显示
        mPaint.setFakeBoldText(true);//是否是粗体
        mPaint.setUnderlineText(true);//是否要下划线
        mPaint.setStrikeThruText(true);//是否需要删除线
        mPaint.setTextSkewX(0.25f);//是否需要文字倾斜
        mPaint.setTextScaleX(5);//均匀拉伸

        canvas.drawText("孤舟独泛，江边望月", 20, 200, mPaint);
    }


}
